<?php

namespace backend\services\recharge;

use backend\extensions\ApiBaseService;
use backend\models\recharge\RechargeRecord;

/**
 * Desc 充值记录服务类
 * @author HUI
 */
class RechargeRecordService extends ApiBaseService
{

    /**
     * 列表
     * @param array $params 参数 
     * **/
    public static function getList($params)
    {
        //检索条件
        $condition = ['AND'];
        if ($params['id']) {
            $condition[] = ['=', 'user_id', $params['id']];
        }
        $list = RechargeRecord::getDataAll($condition, $params['page'], $params['page_size']);
        return $list;
    }

    /**
     * 新增
     * @param array $params 参数 
     * **/
    public static function create($params)
    {
        if (RechargeRecord::create($params) == false) {
            return self::jsonError('保存失败');
        }
        return self::jsonSuccess();
    }

    /**
     * 新增
     * @param array $params 参数 
     * **/
    public static function update($params)
    {
        $condition = ['id' => $params['id']];
        unset($params['id']);
        if (RechargeRecord::updateData($params, $condition) === false) {
            return self::jsonError('更新失败');
        }
        return self::jsonSuccess();
    }

    /**
     * 删除
     * @param array $params 参数 
     * **/
    public static function delete($params)
    {
        $condition = ['id' => $params['id']];
        if (RechargeRecord::del($condition) == false) {
            return self::jsonError('删除失败');
        }
        return self::jsonSuccess();
    }

    /**
     * 详情
     * @param array $params 参数 
     * **/
    public static function detail()
    {
        $condition = [];
        $data = RechargeRecord::getDataOne($condition, '', []);
        return self::jsonSuccess($data);
    }
    
    /**
     * 后台余额调整
     * @param array $params 参数 
     * **/
    public static function recharge($params)
    {
        $transaction = \Yii::$app->db->beginTransaction();
        try {
            $member = \frontend\models\member\Member::findOne(['id'=>$params['id']]);
            if (empty($member)) {
                throw new \Exception('用户信息异常');
            }
            if ($params['type'] == 2 && $member->balance < $params['money']) {
                throw new \Exception('账户余额不足');
            }
            if(empty($member->updateCounters(['balance' => $params['type'] == 1?$params['money']:0-$params['money']]))){
                throw new \Exception('余额调整异常');
            }
            $data = [
                'order' => 0,
                'store' => $params['store'],
                'member' => $member['id'],
                'type' => $params['type'],
                'amount' => $params['money'],
                'balance' => $member->balance,
                'data' => '系统调整',
                'create_time' => date('Y-m-d H:i:s'),
            ];
            $record = new RechargeRecordModel();
            $record->setAttributes($data, false);
            if (empty($record->save())) {
                throw new \Exception('余额支付异常');
            }
            $transaction->commit();
            return self::jsonSuccess();
        } catch (\Exception $exc) {
            $transaction->rollBack();
            return self::jsonError($exc->getMessage());
        }
    }
}
